package cn.wangxl.vertx.starter.web.cookie;

import cn.wangxl.vertx.starter.web.handlebody.BaseBO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Cookie;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;

/**
 * body
 */
public class CookieTest extends AbstractVerticle {

  @Override
  public void start(Future<Void> startFuture) throws Exception {
    Vertx vertx = Vertx.vertx();
    HttpServer server = vertx.createHttpServer();

    Router router = Router.router(vertx);

    router.post("/test")
      .handler(ctx -> {
        HttpServerRequest request = ctx.request();
        HttpServerResponse response = ctx.response();
        int count = ctx.cookieCount();
        if(count>0){
          System.out.println(ctx.cookieMap().toString());

        }
        response.addCookie(Cookie.cookie("yourCookie", "yourCookie")).end("OK");
      });


    server.requestHandler(router).listen(9999, http -> {
      if (http.succeeded()) {
        startFuture.complete();
        System.out.println("HTTP server started on port 8888");
      } else {
        startFuture.fail(http.cause());
      }
    });
  }


  public static void main(String[] args) throws Exception {
    new CookieTest().start(Future.future());
  }
}
